package com.efast.dmom.mgmt.service.mes.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.mgmt.bean.mes.ProductExcuteDto;
import com.efast.dmom.mgmt.dao.mes.TwBasLotTestingDao;
import com.efast.dmom.mgmt.entity.mes.TmBasTestDefinitionItem;
import com.efast.dmom.mgmt.entity.mes.TrBasProductCfgTestitem;
import com.efast.dmom.mgmt.entity.mes.TwBasLot;
import com.efast.dmom.mgmt.entity.mes.TwBasLotTesting;
import com.efast.dmom.mgmt.service.mes.TmBasTestDefinitionItemService;
import com.efast.dmom.mgmt.service.mes.TwBasLotTestingService;
import com.efast.dmom.mgmt.utils.Assist;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TwBasLotTestingServiceImpl implements TwBasLotTestingService{
    @Autowired
    private TwBasLotTestingDao twBasLotTestingDao;
    @Autowired
    private TmBasTestDefinitionItemService tmBasTestDefinitionItemService;
    
    @Override
    public long getTwBasLotTestingRowCount(Assist assist){
        return twBasLotTestingDao.getTwBasLotTestingRowCount(assist);
    }
    @Override
    public List<TwBasLotTesting> selectTwBasLotTesting(Assist assist){
        return twBasLotTestingDao.selectTwBasLotTesting(assist);
    }
    @Override
    public TwBasLotTesting selectTwBasLotTestingByObj(TwBasLotTesting obj){
        return twBasLotTestingDao.selectTwBasLotTestingByObj(obj);
    }
    @Override
    public TwBasLotTesting selectTwBasLotTestingById(Integer id){
        return twBasLotTestingDao.selectTwBasLotTestingById(id);
    }
    @Override
    public int insertTwBasLotTesting(TwBasLotTesting value){
        return twBasLotTestingDao.insertTwBasLotTesting(value);
    }
    @Override
    public int insertNonEmptyTwBasLotTesting(TwBasLotTesting value){
        return twBasLotTestingDao.insertNonEmptyTwBasLotTesting(value);
    }
    @Override
    public int insertTwBasLotTestingByBatch(List<TwBasLotTesting> value){
        return twBasLotTestingDao.insertTwBasLotTestingByBatch(value);
    }
    @Override
    public int deleteTwBasLotTestingById(Integer id){
        return twBasLotTestingDao.deleteTwBasLotTestingById(id);
    }
    @Override
    public int deleteTwBasLotTesting(Assist assist){
        return twBasLotTestingDao.deleteTwBasLotTesting(assist);
    }
    @Override
    public int updateTwBasLotTestingById(TwBasLotTesting enti){
        return twBasLotTestingDao.updateTwBasLotTestingById(enti);
    }
    @Override
    public int updateTwBasLotTesting(TwBasLotTesting value, Assist assist){
        return twBasLotTestingDao.updateTwBasLotTesting(value,assist);
    }
    @Override
    public int updateNonEmptyTwBasLotTestingById(TwBasLotTesting enti){
        return twBasLotTestingDao.updateNonEmptyTwBasLotTestingById(enti);
    }
    @Override
    public int updateNonEmptyTwBasLotTesting(TwBasLotTesting value, Assist assist){
        return twBasLotTestingDao.updateNonEmptyTwBasLotTesting(value,assist);
    }

    public TwBasLotTestingDao getTwBasLotTestingDao() {
        return this.twBasLotTestingDao;
    }

    public void setTwBasLotTestingDao(TwBasLotTestingDao twBasLotTestingDao) {
        this.twBasLotTestingDao = twBasLotTestingDao;
    }
    
    //保存产品执行中TwBasLotTesting的测试数值
	@Override
	public int saveLotTesting(ProductExcuteDto dto) {
		String currentUser = SpringWebUtils.getRequestUser();
		String companyCode = SpringWebUtils.getRequestCompany();
		TwBasLot lot = dto.getRowData();
		List<TwBasLotTesting> testingList = new ArrayList<TwBasLotTesting>();
		String defItemIds = "";
		for(TrBasProductCfgTestitem i:dto.getTesting()) {
			defItemIds+=i.getTestItemId()+",";
		}
		if(StringUtils.isNotBlank(defItemIds)) {
			defItemIds = defItemIds.substring(0,defItemIds.length()-1);
		}
		//获取测试定义item，用map返回
		Map<Integer,Object> testDefinitionItemMap = tmBasTestDefinitionItemService.getTestDefinitionItemByids(defItemIds);
		for(TrBasProductCfgTestitem test:dto.getTesting()) {
			TwBasLotTesting testing = new TwBasLotTesting();
			TmBasTestDefinitionItem defItem = (TmBasTestDefinitionItem) testDefinitionItemMap.get(test.getTestItemId());
			testing.setProductCfgItemId(test.getProductCfgItemId());
			testing.setValueType(defItem.getValueType());
			testing.setRepairResult("");//维修结果
			testing.setLotName(lot.getLotName());
			testing.setOffseth(defItem.getOffseth());
			testing.setTestValue(test.getTestValue());
			testing.setOffsetl(defItem.getOffsetl());
			testing.setLotId(lot.getLotId());
			testing.setTestResult("");//测试结果
			testing.setPassRule(defItem.getPassRule());
			testing.setLimitl(defItem.getLimitl());
			testing.setLimith(defItem.getLimith());
			testing.setStandardValue(String.valueOf(defItem.getStandardValue()));
			testing.setStatus(defItem.getStatus());
			testing.setOperationId(lot.getOperationId());
			testing.setComments(defItem.getComments());
			testing.setCreateTime(new Date());
			testing.setCreateUser(currentUser);
			testing.setCompanyCode(companyCode);
			testing.setOptCounter(defItem.getOptCounter());
			testing.setMarkStatus(defItem.getMarkStatus());
			testingList.add(testing);
		}
		return twBasLotTestingDao.insertTwBasLotTestingByBatch(testingList);
	}
	
	//批量更新产品执行中TwBasLotTesting的测试数值
	@Override
	public int updateLotTestingValueByBatch(List<TwBasLotTesting> testingList, ProductExcuteDto dto) {
		for(TrBasProductCfgTestitem test:dto.getTesting()) {
			if(testingList!=null&&testingList.size()>0) {
				for(int i=0;i<testingList.size();i++) {
					if(testingList.get(i).getProductCfgItemId().equals(test.getProductCfgItemId())) {
						testingList.get(i).setTestValue(test.getTestValue());
					}
				}
			}
		}
		return twBasLotTestingDao.updateLotTestingValueByBatch(testingList);
	}

}